/*
   FtpCmdFiles.java
*/

import java.util.*;
import java.io.*;

public class FtpCmdFiles
{

  private static String mdir;
  private static PrintStream mout;

  static
  {
    mout = System.out;
  }

//===================================================

  static void setOutStream(PrintStream out)
  {
    mout = out;
  }

//===================================================

  public FtpCmdFiles(String dir)//Constructor
  {
    mdir = dir;
    validate();
  }

//==================================================

  public static void setDir(String dir)
  {
    mdir = dir;
    validate();
  }

//===================================================

  public static void listDirectory()
  {
    try
    {
      File dir = new File(mdir); 
      File files[] = dir.listFiles();
      for(int i = 0; i < files.length; i++)
      {
        if( files[i] != null && !files[i].isHidden())//true if file is not hidden
        {
          mout.printf(
              "%c%c\t%d\t%s\t%s\n",
              (files[i].canRead() == true) ? 'r' : '-',
              (files[i].canWrite() == true) ? 'w': '-',
              files[i].length(),
              new Date(files[i].lastModified()), 
              files[i].getName());
        }
      }
    }
    catch (NullPointerException e)
    {
      System.out.printf("Error in listDirectory()\n");
      System.out.printf("Exception: %s\n", e);
      e.printStackTrace() ;
      return;
    }
  }

//===================================================

  public static void validate()
  {
    if(mdir.charAt(mdir.length()-1) != '/')
    {
      mdir += '/';
    }
  }

//===================================================

  public static boolean cdAbsolute(String newdir)
  {
    File file = new File(newdir);
    if(file.exists() && file.isDirectory())
    {
      setDir(newdir);
      return true;
    }
    else
      return false;
  }

//===================================================

  public static boolean cdRelative(String path)
  {
    if(path == "..")
    {
      File curr = new File(mdir);
      if(curr.getParent() != null)
        mdir = curr.getParent();
      validate();
      return true;
    }
    else
    {
      File curr = new File(mdir + path);
      if(curr.exists())
      {
        mdir = mdir + path;
        validate();
        return true;
      }
      else
      {
        return false;
      }
    }
  }

//===================================================

  public String getDir()
  {
    return mdir;
  }

//===================================================

  public String lpwd()
  {
    File path = new File(mdir);
    try { return path.getCanonicalPath(); } catch(IOException e) {}
    return "";
  }

//===================================================

  static boolean mkDir(String out)
  {
    boolean  dir;
    File mf = new File(out);
    if(!mf.exists())
    {
      dir = mf.mkdir();
      return dir;
    }
    else
    {
      mout.printf("Directory '%s' already exists. \n", mf);
      return false;
    }
  }

//===================================================

  static boolean rmDir(String out)
  {
    String ans = "yes";
    boolean dir = false; 
    File f = new File(out);
    if(f.exists())
    {
      if(FtpCommon.getPrompt())
        ans = ScreenIO.promptForString("Are you sure you want to delete "+out+"? [yes/no]: ");
      if(ans.equals("yes"))
        dir = f.delete();
      return dir;
    }
    else
      mout.printf("Directory '%s' does not exist. \n", f);
    return false; 
  }

//===================================================

  static boolean rmFile(String ftd)
  {
    String ans = "yes";
    boolean ret = false;
    File file = new File(mdir + ftd);
    if(file.exists())
    {
      if(FtpCommon.getPrompt())
        ans = ScreenIO.promptForString("Are you sure you want to delete "+ftd+"? [yes/no]: ");
      if(ans.equals("yes"))
        ret = file.delete();
      return ret;
    }
    else
      System.out.println("\tFile Does Not Exist");
    return false;
  }
}
